<template >
  <div class="add_con_form tl" v-loading="loading">
    <el-dialog :close-on-click-modal='false' :title="mytitle" :modal-append-to-body="false" :visible.sync="visible" :show="show"
        :before-close="closeDialog" @close="$emit('update:show',false)" class="tl size_13" width="800px">
      <el-form :model="data" :rules="dataYZ" ref="data" class="mar_top" label-width="120px">
        <el-form-item label="店名" prop="title">
          <el-col :span="12">
            <el-input placeholder="" v-model="data.title" clearable>
            </el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="联系人姓名" prop="name">
          <el-col :span="12">
            <el-input placeholder="" v-model="data.name" clearable>
            </el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="联系人手机" prop="tel">
          <el-col :span="12">
            <el-input placeholder="" v-enterNumber v-model="data.tel" clearable>
            </el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="设置店长" prop="username">
          <el-col :span="12">
            <el-input placeholder="请选择用户" v-model="data.username" :disabled="true" clearable></el-input>
          </el-col>
          <el-col :span="5" :offset="1">
            <el-button :loading="$store.state.isLoading" size="medium" type="primary" @click="xzshop('xzuser')">选择用户</el-button>
          </el-col>
        </el-form-item>

        <el-form-item label="封面图" prop="piclink">
          <el-col :span="12">
            <el-input v-model="data.piclink" disabled autocomplete="off"></el-input>

            <compressimgUpload :onSuccess="onSuccess" :pic="data.piclink" :isOneImg="true" :files="fileList"></compressimgUpload>
          </el-col>
        </el-form-item>

        <el-form-item label="所在省市区" prop="province">
          <el-col :span="12">

            <city-cascader size='large' :placeholder='plad' :level='2' @change="handleChange" />

          </el-col>
        </el-form-item>

        <el-form-item label="地址" prop="add">
          <el-col :span="12">
            <el-input placeholder="" v-model="data.add" clearable>
            </el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="经纬度">
          <el-col :span="12">
            <el-input placeholder="" v-model="JWD" clearable>
            </el-input>
          </el-col>
          <el-col :span="5" :offset="1">
            <a target="_blank" :href="'https://lbs.qq.com/tool/getpoint/index.html'">
              <el-button :loading='$store.state.isLoading' size="mini">查看经纬度</el-button>
            </a>
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button :loading='$store.state.isLoading' size="medium" type="primary" @click.prevent="subForm('data')">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <keep-alive>
      <component :is="current" :show="shows" :isAll="false" :isPart="true" showType="list" @sendShop="getSend" @isshow="getShow"></component>
    </keep-alive>

  </div>
</template>


<style lang="less" scoped>
</style>

<script>
import compressimgUpload from "@/components/currency/compressimgUpload.vue";
import cityCascader from "@/components/currency/cityCascader/cityCascader";
import xzuser from "@/components/currency/xz_user.vue";
export default {
  components: {
    compressimgUpload,
    cityCascader,
    xzuser
  },
  data () {
    var checkTitle = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("标题不能为空"));
      } else {
        callback();
      }
    };

    return {
      mytitle: "",
      fileList: [],
      updateUrl: "",
      showImg: true,
      options: [],
      selectedOptions: [], //["福建省", "福州市", "台江区"]
      JWD: "",
      optionProps: {
        value: "y",
        label: "t",
        children: "z"
      },
      value: "",
      data: {
        sid: "",
        name: "",
        tel: "",
        title: "",
        piclink: "",
        province: "",
        city: "",
        area: "",
        town: "",
        add: "",
        longitude: "",
        latitude: ""
      },
      plad: "",
      loading: false,
      dataYZ: {
        title: [
          { required: true, message: "请输入店名", trigger: "blur" },
          { min: 2, max: 12, message: "长度在 2 到 12 个字符", trigger: "blur" }
        ],
        name: [
          { required: true, message: "请输入联系人姓名", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" }
        ],
        tel: [
          { required: true, message: "请输入联系人电话", trigger: "blur" },
          { min: 11, max: 11, message: "长度在 11 个字符", trigger: "blur" }
        ],
        add: [{ required: true, message: "请输入地址", trigger: "blur" }]
      },
      visible: this.show,
      current: "",
      shows: false,
    };
  },
  props: {
    show: Boolean,
    editId: String,
    clickNum: Number
  },
  methods: {
    xzshop (str) {
      this.current = str;
      this.shows = true;
    },
    getShow (val) {
      this.shows = val;
    },
    getSend (row, obj) {
      if (obj == 'obj') {
        var str = "";
        for (let i of row) {
          str += i.username + ",";
        }
        str = str.slice(0, -1);
        this.data.username = str;
      } else {
        this.data.username = row.username;
      }
    },
    onSuccess (pic) {
      this.data.piclink = pic;
      this.showImg = true;
      this.updateUrl = this.$fnc.getImgUrl(pic);
    },
    handleChange (value) {
      if (value) {
        for (var i = 0; i < value.length; i++) {
          if (i == 0) this.data.province = value[i];
          if (i == 1) this.data.city = value[i];
          if (i == 2) this.data.area = value[i];
          if (i == 3) this.data.town = value[i];
        }
      }
    },

    closeDialog () {
      this.visible = false;
    },
    getDetails (id) {
      var data = {};
      data["id"] = id;
      this.$api.getUser.reserve_store_edit(data).then(res => {
        this.data = res.data.result;
        if (this.data.piclink.indexOf("http") >= 0) {
          this.updateUrl = this.data.piclink;
        } else {
          this.updateUrl = "/api/" + this.data.piclink;
        }
        this.selectedOptions = [
          this.data.province,
          this.data.city,
          this.data.area
        ];
        if (this.data.town) {
          this.selectedOptions.push(this.data.town);
        }
        this.plad = this.selectedOptions.join("-");
        this.JWD = this.data.latitude + "," + this.data.longitude;
      });
    },
    subForm (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.editId == undefined) {
            this.AddNews(formName);
          } else {
            this.UpdateNews(formName);
          }
        }
      });
    },
    AddNews () {
      var JWD = this.JWD;
      var jwd_ar = JWD.split(",");
      this.data.longitude = jwd_ar[1]; //经度
      this.data.latitude = jwd_ar[0]; //纬度
      this.$api.getUser.reserve_store_saveadd(this.data).then(res => {
        if (res.data.status == 1 && res.data.code == 200) {
          this.$fnc.alertSuccess({ text: "添加成功" });
          this.$emit("parent", "这里刷新父页面");
          this.visible = false;
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
    UpdateNews (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var params = {};
          params["id"] = this.editId;
          params["name"] = this.data.name;
          params["tel"] = this.data.tel;
          params["title"] = this.data.title;
          params["province"] = this.data.province;
          params["city"] = this.data.city;
          params["area"] = this.data.area;
          params["town"] = this.data.town;
          params["piclink"] = this.data.piclink;
          params["add"] = this.data.add;
          var JWD = this.JWD;
          var jwd_ar = JWD.split(",");
          params["longitude"] = jwd_ar[1]; //经度
          params["latitude"] = jwd_ar[0]; //纬度
          params["username"] = this.data.username;

          this.$api.getUser.reserve_store_saveedit(params).then(res => {
            if (res.data.code == 200 && res.data.result != null) {
              this.$emit("parent", "这里刷新父页面");
              this.$fnc.alertSuccess({ text: "编辑成功" }); //成功弹出框
              this.visible = false;
            } else {
              this.$fnc.alertError(res.data.result); //失败弹出框
            }
          });
        }
      });
    }
  },
  watch: {
    show () {
      this.visible = this.show;
    },
    clickNum (id) {
      if (this.editId !== undefined) {
        this.mytitle = "编辑";
        this.getDetails(this.editId);
      } else {
        this.mytitle = "添加";
        if (this.$refs["data"] !== undefined) {
          this.$refs["data"].resetFields();
        }
        this.selectedOptions = [];
        this.JWD = "";
        this.updateUrl = "";
        this.showImg = false;
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.options = [];
    });
  },
  created () { },
  updated () { }
};
</script>
